<template>
  <div style="padding:20px">
    <el-card>
      <h1>分类列表</h1>
    <el-table :data = "$store.state.category" border stripe>
      <el-table-column type = "expand">
        <template slot-scope="scope">
          <div style = "position:relative;left:30px">
            <el-tag
            :key="i"
            v-for="(tag, i) in scope.row.children"
            closable
            :disable-transitions="false"
            @close="saveList(scope.row,'删除',i)">
            {{tag}}
          </el-tag>
          <el-input
            class="input-new-tag"
            v-if="scope.row.inputVisible"
            v-model="scope.row.inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="handleInputConfirm(scope.row)"
            @blur="handleInputConfirm(scope.row)"
          >
          </el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop = "name" label = "分类名称"></el-table-column>
      <el-table-column prop = "icon" label = "分类图标"></el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="180">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="edit(scope.row)">编辑</el-button>
          <el-button type="primary" size="small" @click="remove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  methods: {
    async remove (row) {
      this.$confirm(`是否确定删除分类?"${row.name}"`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$http.delete(`rest/Cate/${row._id}`).then(res => {
          if (res.status !== 200) {
            return this.$message.error('删除失败！')
          }
          this.$store.commit('deleteCate', row._id)
          this.$message.success('删除成功！')
        })
      })
    },
    showInput (row) {
      row.inputVisible = true
    },
    handleInputConfirm (row) {
      if (row.inputValue.trim().length === 0) {
        row.inputValue = ''
        row.inputVisible = false
      } else {
        this.saveList(row, '添加')
        row.inputVisible = false
        row.inputValue = ''
      }
    },
    async saveList (row, way, i) {
      if (way === '添加') {
        row.children.push(row.inputValue)
      } else if (way === '删除') {
        row.children.splice(i, 1)
      }
      await this.$http.put('rest/Cate', row).then(res => {
        if (res.status !== 200) {
          return this.$message.error(way + '失败！')
        }
        this.$message.success(way + '成功！')
      })
    },
    edit (row) {
      window.sessionStorage.setItem('activePath', '/categories/create')
      this.$router.push(`/categories/edit/${row._id}`)
    }
  }
}
</script>

<style scoped>
  .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
